package com.briup.cms.service.impl;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import com.briup.cms.constant.CmsConstants;
import com.briup.cms.dao.ArticleMapper;
import com.briup.cms.dao.extend.ArticleExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 资讯信息服务层
 */
@Service
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleExtendMapper articleExtendMapper;


    // 通过id查询咨询信息，同时获取咨询对应的栏目和用户信息
    @Override
    public ArticleExtend findByIdWithCategoryAndUser(int id) {
    return articleExtendMapper.findByIdWithCategoryAndUser(id);
    }

    // 保存或更新咨询信息
    @Transactional
    @Override
    public void saveOrUpdate(Article article) throws CustomerException {
        if(article.getArticleId()!=null){
            Article dbArticle = articleMapper.selectByPrimaryKey(article.getArticleId());
            if(dbArticle == null) {
                throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
            }
            articleMapper.updateByPrimaryKeySelective(article);
        }
        // id为空，就是保存
        else {
            // 不齐字段值
            article.setArticlePublishTime(new Date());
            article.setArticleStatus(CmsConstants.ARTICLE_STATUS_UNCHECKED);
            article.setArticleThumpUp(0);
            article.setArticleReadTimes(0);
            articleMapper.insert(article);
        }
    }


    // 分页查询咨询信息，同时获取咨询对应的栏目和用户信息
    @Override
    public PageInfo<ArticleExtend> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize,String title,Integer status,Integer categoryId) throws CustomerException {
        PageHelper.startPage(pageNum,pageSize,true);

        List<ArticleExtend> list = articleExtendMapper.queryArticleWithCategoryAndUser(title,status,categoryId);
        if(list == null){
            list = new ArrayList<>();
        }

        return new PageInfo<>(list);
    }


    // 通过id删除咨询信息
    @Transactional
    @Override
    public void deleteById(int id) throws CustomerException {
        articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryRecommend(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<ArticleExtend> list = articleExtendMapper.findCategoryByStatus(CmsConstants.ARTICLE_STATUS_RECOMMEND);
        return new PageInfo<>(list);
    }

    // 修改咨询信息的状态，状态：0-待审核，1-不通过，2-通过，3-推荐
	@Transactional
	@Override
    public void changeStatus(int id, int status) throws CustomerException {
        Article artileFromDB = articleMapper.selectByPrimaryKey(id);
        if(artileFromDB == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
        }

        Article article = new Article();
        article.setArticleId(id);
        article.setArticleStatus(status);

        articleMapper.updateByPrimaryKeySelective(article);

    }
}

